#include "marker.h"

#include <QSharedData>
#include <QString>

class MarkerData : public QSharedData
{
public:
    QUuid uuid;
    qint64 position;
    QString name;
};

Marker::Marker() : data(new MarkerData)
{
    data->uuid = QUuid::createUuid();
    setPosition(-1);
}

Marker::Marker(qint64 position) : data(new MarkerData)
{
    data->uuid = QUuid::createUuid();
    setPosition(position);
}

Marker::Marker(qint64 position, QString name) : data(new MarkerData)
{
    data->uuid = QUuid::createUuid();
    setPosition(position);
    setName(name);
}

Marker::Marker(const Marker &rhs) : data(rhs.data)
{
}

Marker &Marker::operator=(const Marker &rhs)
{
    if (this != &rhs)
        data.operator=(rhs.data);
    return *this;
}

Marker::~Marker()
{
}

bool Marker::operator ==(const Marker& rhs) const
{
    return position() == rhs.position() && name() == rhs.name();
}

bool Marker::operator <(const Marker& rhs) const
{
    return position() < rhs.position();
}

QUuid Marker::uuid() const
{
    return data->uuid;
}

void Marker::setPosition(qint64 position)
{
    data->position = position;
}

qint64 Marker::position() const
{
    return data->position;
}

void Marker::setName(QString name)
{
    data->name = name;
}

QString Marker::name() const
{
    return data->name;
}

bool Marker::isValid() const
{
    return data->position != -1;
}

QDataStream& operator<<(QDataStream& stream, const Marker& marker)
{
    return stream << marker.name() << marker.position();
}

QDataStream& operator>>(QDataStream& stream, Marker& marker)
{
    QString name;
    qint64 position;
    stream >> name >> position;
    marker.setName(name);
    marker.setPosition(position);
    return stream;
}
